#include<bits/stdc++.h>
#define gc getchar
#define itn int
#define x first
#define y second
#define eb emplace_back
#define em emplace
#define pb push_back
#define db double
#define y1 yy1_yyds
using namespace std; typedef long long ll; typedef unsigned long long ull;
// https://www.luogu.com.cn/discuss/522581 About "const"
ll read() {
	ll x = 0; short fh = 1; char ch = gc();
	while (!isdigit(ch)) {
		if (ch == '-') fh = -1;
		if (ch < 10) exit(0); 
		ch = gc();
	}
	while (isdigit(ch))
		x = x * 10 + (ch ^ 48), ch = gc();
	return fh * x;
}
#ifndef ONLINE_JUDGE
void debug() {cerr << "\n";}
template<typename Typ1> void debug(Typ1 arg) {cerr << arg << "\n";}
template<typename Typ1, typename ...Typ2> void debug(Typ1 arg, Typ2 ...args) {
	cerr << arg << " ", debug(args...);
}
#else
void debug() {}
template<typename Typ1> void debug(Typ1 arg) {}
template<typename Typ1, typename ...Typ2> void debug(Typ1 arg, Typ2 ...args) {}
#endif
void writeln(ll arg) {printf("%lld\n", arg);}
template<typename ...Typ2> void writeln(ll arg, Typ2 ...args) {
	printf("%lld ", arg), writeln(args...);
}
typedef pair <int, int> pii; typedef pair <ll, ll> pll;
const char Y_E_S[] = "YES", N__O[] = "NO";
// const char Y_E_S[] = "Yes", N__O[] = "No";
// #define infinite_testcase
// #define multiple_testcase
// #define output_Yes_No
const int DUST = 327, N = 415, M = -1;
ll ans = 0;
int p[N]; bool vis[N];
int n;
vector<int> e[N];
ll a[N]; ll b[N];
ll calc() {
	ll res = 0;
	memcpy(b, a, sizeof(ll) * (n + 2));
	for(int i = 1; i <= n; i++) {
		int u = p[i];
		res += b[u];
		for(int v : e[u]) b[v] += b[u];
	}
	ans = max(ans, res);
	return res;
}
void dfs(int dph) {
	if(dph == n + 1) return calc(), void();
	for(int i = 1; i <= n; i++) if(!vis[i]) {
		vis[p[dph] = i] = 1;
		dfs(dph + 1);
		vis[i] = 0;
	}
}
mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
bool major(int Case = 1) {
	n = read();
	for(int i = 1; i <= n; i++) a[i] = read();
	for(int i = 1; i < n; i++) {
		int u = read(), v = read();
		e[u].pb(v), e[v].pb(u);
	}
	if(n <= 10) dfs(1);
	else {
		for(int i = 1; i <= n; i++) p[i] = i;
		while(clock() / (db)CLOCKS_PER_SEC <= 0.9) {
			shuffle(p + 1, p + n + 1, rnd);
			calc();
		}
	}
	writeln(ans);
	debug(clock(), rnd());
	return Case ^= Case ^ Case;
}
void initial_function(int argc, char **argv) {
	**argv = argc; /* <- place_holder
	you won't give up no matter what happens, will you?
	code time: 00:
	---
	
	*/
	freopen("light.in", "r", stdin);
	freopen("light.out", "w", stdout);
}
signed main(int argc, char **argv) {
	initial_function(argc, argv);
	int Case = 1, Maxcase = 1;
	for (
#ifdef multiple_testcase
		  Maxcase = read()
#endif
				     	  ;
#ifndef infinite_testcase
							Case <= Maxcase
#endif
				     					   ; Case++)
#ifdef output_Yes_No
		puts(major(Case) ? Y_E_S : N__O);
#else
		major(Case);
#endif
	return DUST ^ 0x147;
}
